/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-08     Administrator       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <bsp_key.h>




static struct key keys[3] = {
    {KEY1_PIN, 0, 0, 0, KEY_EVENT_NONE},
    {KEY2_PIN, 0, 0, 0, KEY_EVENT_NONE},
    {KEY3_PIN, 0, 0, 0, KEY_EVENT_NONE}

};

/* 按键配置参数 */
#define KEY_DEBOUNCE_TIME   20     // 消抖时间(ms)
#define KEY_LONG_PRESS_TIME 1000   // 长按判定时间(ms)
#define KEY_DOUBLE_TIME     300    // 双击间隔时间(ms)

/* 按键初始化 */
void keys_init(void)
{
    for (int i = 0; i < 3; i++) {
        rt_pin_mode(keys[i].pin, PIN_MODE_INPUT_PULLUP);
    }
}

/* 按键扫描函数 (每10ms调用一次) */
void keys_scan(void)
{
    static rt_tick_t last_scan = 0;
    rt_tick_t now = rt_tick_get();

    /* 每10ms扫描一次 */
    if (now - last_scan < rt_tick_from_millisecond(10)) {
        return;
    }
    last_scan = now;

    for (int i = 0; i < 3; i++) {
        rt_uint8_t pin_state = rt_pin_read(keys[i].pin);

        /* 状态0: 等待按下 */
        if (keys[i].state == 0) {
            if (pin_state == 0) {  // 按下(低电平)
                keys[i].state = 1; // 进入按下状态
                keys[i].press_time = now;
            }
        }
        /* 状态1: 按下状态 */
        else if (keys[i].state == 1) {
            /* 检查是否长按 */
            if (now - keys[i].press_time > rt_tick_from_millisecond(KEY_LONG_PRESS_TIME)) {
                keys[i].event = KEY_EVENT_LONG_PRESS;
                keys[i].state = 3; // 长按状态
            }
            /* 检查是否释放 */
            else if (pin_state == 1) { // 释放(高电平)
                keys[i].state = 2;     // 进入释放状态
                keys[i].last_time = now;
            }
        }
        /* 状态2: 释放状态(等待单击或双击) */
        else if (keys[i].state == 2) {
            /* 检查是否双击 */
            if (pin_state == 0) { // 再次按下
                keys[i].event = KEY_EVENT_DOUBLE_CLICK;
                keys[i].state = 3; // 进入双击状态
            }
            /* 检查单击超时 */
            else if (now - keys[i].last_time > rt_tick_from_millisecond(KEY_DOUBLE_TIME)) {
                keys[i].event = KEY_EVENT_CLICK;
                keys[i].state = 0; // 返回空闲状态
            }
        }
        /* 状态3: 长按/双击状态(等待释放) */
        else if (keys[i].state == 3) {
            if (pin_state == 1) { // 释放
                keys[i].state = 0; // 返回空闲状态
            }
        }
    }
}

/* 获取按键事件 */
enum key_event get_key_event(int key_index)
{
    if (key_index < 0 || key_index > 2) return KEY_EVENT_NONE;

    enum key_event event = keys[key_index].event;
    keys[key_index].event = KEY_EVENT_NONE; // 读取后清除事件
    return event;
}

/* 按键测试函数 */
void keys_test(void)
{
    keys_init();

    while (1) {
        keys_scan();
        for (int i = 0; i < 3; i++) {
            enum key_event event = get_key_event(i);
            if (event != KEY_EVENT_NONE) {
                rt_kprintf("Key%d: ", i+1);

                switch (event) {
                case KEY_EVENT_CLICK:
                    rt_kprintf("Click\n");
                    if (i == 0 ) {
                        led_control(LED_B_PIN,1);
                    }
                    else if (i == 1) {
                        led_control(LED_G_PIN,1);
                    }
                    else if (i == 2) {
                        rgb_led_control(1, 1, 1);
                    }

                    break;
                case KEY_EVENT_DOUBLE_CLICK:
                    rt_kprintf("Double Click\n");
                    if (i == 0 ) {
                            led_control(LED_B_PIN,0);
                        }
                        else if (i == 1) {
                            led_control(LED_G_PIN,0);
                        }
                        else if (i == 2) {
                            rgb_led_control(0, 0, 0);
                        }
                    break;
                case KEY_EVENT_LONG_PRESS:
                    rt_kprintf("Long Press\n");
                    break;
                default:
                    break;
                }
            }
        }
        rt_thread_mdelay(10);
    }
}

/* 导出到MSH命令 */
MSH_CMD_EXPORT(keys_test, Keys test command);
